package gestion.Controlador;

import gestion.negocio.GestionUsuario;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Usuario
 */
@WebServlet("/Usuario")
public class Usuario extends HttpServlet {
	private static final long serialVersionUID = 1L;

    
    public Usuario() {
        // TODO Auto-generated constructor stub
    }
    
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Collection<gestion.modelo.Usuario> persona=null;
		GestionUsuario ges = new GestionUsuario();
		
		
		String nombre = request.getParameter("username");
		String apeP = request.getParameter("apep");
		String apeM = request.getParameter("apem");
		String sexo = request.getParameter("expiry_month");
		char sex =sexo.charAt(0);
		String dni = request.getParameter("dni");
		String correo = request.getParameter("email3");
		String perfil = request.getParameter("perfil");
		int perf = Integer.parseInt(perfil);
		String clave = request.getParameter("clave");
		
		if(nombre=="" || apeP =="" || correo=="" || clave==""){
			
			
			String aviso = "Porfavor Llene Todos los campos!";
			request.setAttribute("errorN", aviso);
			
			RequestDispatcher rd = request.getRequestDispatcher("NuevoUsuario.jsp");
			rd.forward(request, response);	
			
		}
		
		else{
		GestionUsuario user = new GestionUsuario();
		
		try {
			user.insertar(perf, 1, nombre, apeP, apeM, sex, "1", dni, correo, "789654123", clave);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			persona = ges.listar();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    request.setAttribute("listar", persona);
		
		RequestDispatcher rd = request.getRequestDispatcher("Menu.jsp");
		rd.forward(request, response);	
		
		
		
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Collection<gestion.modelo.Usuario> persona=null;
		GestionUsuario ges = new GestionUsuario();
		
		String correo = request.getParameter("correo");
		String clave = request.getParameter("clave");
		System.out.println(correo);
		System.out.println(clave);
		
		gestion.modelo.Usuario user = null;
		
		GestionUsuario u = new GestionUsuario();
		try {
		 user = u.Logeo(correo, clave);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 	
		System.out.println(user.getCorreo());
		System.out.println(user.getClave());
		
		if(user.getCorreo()!=null){
			
			
				try {
					persona = ges.listar();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			request.setAttribute("listar", persona);
			
			HttpSession sesion = request.getSession(true);

            sesion.setAttribute("user",user.getNombres());
             System.out.println(user.getNombres());
			RequestDispatcher rd = request.getRequestDispatcher("Menu.jsp");
			rd.forward(request, response);
		}
		else{
			String aviso = "No Existe";
			request.setAttribute("error", aviso);
			
			RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
			rd.forward(request, response);
			
		}
			
		
	}

}
